package com.tech.view.component.input;

import com.tech.view.common.ResourcesUtil;
import com.tech.view.common.SwingTestUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * 整数输入框，只支持整数
 * <p>
 * 可以规定最大值和最小值，超过最大值时会禁止数继续增加，小于最小值并且输入框失去焦点时会变为最小值
 */
public class IntegerInputField extends JComponent implements KeyListener, FocusListener {
	public static int DEFAULT_WIDTH = 72;
	public static int DEFAULT_HEIGHT = 24;
	private int width;
	private int height;
	private int currentNum;
	private int maxNum;
	private int minNum;
	private JTextField textField;
	private JButton subButton;
	private JButton addButton;

	/**
	 * 初始值为0，不规定最大值和最小值的数字输入框
	 */
	public IntegerInputField() {
		this(DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
	}

	/**
	 * 初始值为最小值的数字输入框
	 *
	 * @param minNum 最小值
	 * @param maxNum 最大值
	 */
	public IntegerInputField(int minNum, int maxNum) {
		this(DEFAULT_WIDTH, DEFAULT_HEIGHT, minNum, minNum, maxNum);
	}

	/**
	 * @param originNum 初始值
	 * @param minNum    最小值
	 * @param maxNum    最大值
	 */
	public IntegerInputField(int width, int height, int originNum, int minNum, int maxNum) {
		if (originNum < minNum) throw new IllegalArgumentException("初始值不不能低于最小值");
		this.width = width;
		this.height = height;
		currentNum = originNum;
		this.maxNum = maxNum;
		this.minNum = minNum;

		init();
	}

	public static void main(String[] args) {
		SwingTestUtils.loadSkin();
		IntegerInputField integerInputField = new IntegerInputField(10, 999);
		JButton button = new JButton("获取");
		button.addActionListener(e -> System.out.println(integerInputField.getNum()));
		SwingTestUtils.test(integerInputField, button);
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(width, height);
	}

	private void init() {
		setPreferredSize(new Dimension(width, height));
		setLayout(null);

		textField = new JTextField(currentNum + "");
		textField.setBounds(0, 0, width - 20, height);
		add(textField);

		addButton = new JButton(new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.ADD_DIG)));
		addButton.setBounds(width - 20, 0, 20, height / 2);
		subButton = new JButton(new ImageIcon(ResourcesUtil.getImage(ResourcesUtil.Image.SUB_DIG)));
		subButton.setBounds(width - 20, height / 2, 20, height / 2);
		add(addButton);
		add(subButton);

		addListener();
	}

	private void addListener() {
		addButton.addActionListener(e -> addOne());
		subButton.addActionListener(e -> subOne());

		textField.addKeyListener(this);
		textField.addFocusListener(this);
	}

	public void addOne() {
		if (currentNum + 1 <= maxNum) {
			currentNum++;
			textField.setText(currentNum + "");
		}
	}

	public void subOne() {
		if (currentNum - 1 >= minNum) {
			currentNum--;
			textField.setText(currentNum + "");
		}
	}

	public int getNum() {
		return currentNum;
	}

	public void setNum(int newNum) {
		currentNum = newNum;
		textField.setText(newNum + "");
	}

	// 值应与getNum获取到的结果是一样的
	public String getText() {
		return textField.getText();
	}

	@Override
	public void keyTyped(KeyEvent e) {
		char key = e.getKeyChar();
		if (!(key >= '0' && key <= '9') && key != '-') e.consume(); // 非数字输入不接受
	}

	@Override
	public void keyPressed(KeyEvent e) {
	}

	@Override
	public void keyReleased(KeyEvent e) {
		int newNum = 0;
		String text = textField.getText();
		if ("".equals(text) || "-".equals(text)) newNum = 0;
		else {
			try {
				newNum = Integer.parseInt(text);
			} catch (NumberFormatException ex) {
				textField.setText("0"); //一次输入超过int
			}
		}

		if (newNum > maxNum) textField.setText(currentNum + "");
		else currentNum = newNum;
	}

	@Override
	public void focusGained(FocusEvent e) {
		textField.selectAll();
	}

	@Override
	public void focusLost(FocusEvent e) {
		if (currentNum < minNum) setNum(minNum);
	}
}
